<script>
/*****************************************/
/* Initializing impex adapter for JQgrid */
/*****************************************/

var grids = {};

var defaultsWithPager = {
    height : 300,
    datatype : "json",
    rowNum : 50,
    rowList : [20, 30, 50],
    prmNames : {
        page : "pg"
    }
};

var defaults = {
    height : 300,
    datatype : "json",
    rowNum : 5000,
    prmNames : {
        page : "pg"
    }
};

function adoptGridData(gridId) {
    var sortable = false;
    var hasPager = false;

    if (grids[gridId]) {
        if (jQuery("div#" + gridId + "_pager").size() > 0) {
            grids[gridId].pager = "#" + gridId + "_pager";
            hasPager = true;
        }
        if (hasPager) {
            for (var fld in defaultsWithPager) {
                if (typeof(defaultsWithPager[fld]) == "function") { continue; }
                if (grids[gridId][fld] !== undefined) { continue; }
                grids[gridId][fld] = defaultsWithPager[fld];
            }
        } else {
            for (var fld in defaults) {
                if (typeof(defaults[fld]) == "function") { continue; }
                if (grids[gridId][fld] !== undefined) { continue; }
                grids[gridId][fld] = defaults[fld];
            }
        }
        if (typeof(window[gridId + "_selected"]) == "function") {
            grids[gridId].onSelectRow = function(rowid, status) {
                eval(gridId + "_selected(rowid)");
            };
        }
    }
    jQuery("table.admingrid#" + gridId).jqGrid(grids[gridId]);
    if (grids[gridId].sortable) {
        jQuery("table.admingrid#" + gridId).jqGrid("sortableRows", {
            connectWith : "table.admingrid#" + gridId,
            update : function(e, ui) {
                callReorder(gridId);
            }
        });
    }
    refreshGrid(gridId);
}

function refreshGrid(gridId) {
    jQuery("table.admingrid#" + gridId).jqGrid().trigger("reloadGrid");
}

function callReorder(gridId) {
    var fn = "reorder_" + gridId;
    var param = jQuery("table.admingrid#" + gridId).jqGrid("getDataIDs");
    if (typeof window[fn] == "function") {
        eval(fn + "(param)");
    }
}

function getSelectedRow(gridId) {
    return jQuery("table.admingrid#" + gridId).jqGrid("getGridParam", "selrow");
}

function createGrids() {
    for (var grid in grids) {
        if (typeof(grids[grid]) == "function") { continue; }
        adoptGridData(grid);
    }
}

function getSelectedRows(gridId) {
    return jQuery("table.admingrid#" + gridId).jqGrid().getGridParam("selarrrow");
}

function getGridCount(gridId) {
    return jQuery("table.admingrid#" + gridId).jqGrid().getGridParam("reccount");
}

function setGridData(gridId, data) {
    jQuery("table.admingrid#" + gridId).jqGrid("clearGridData");
    jQuery("table.admingrid#" + gridId).jqGrid("setGridParam", {
        datatype : "local",
        data : data.rows,
        rowNum : data.rows.length
    });
    refreshGrid(gridId);
}

function resetSelection(gridId) {
    jQuery("table.admingrid#" + gridId).jqGrid("resetSelection");
}

</script>
